<?php
/**
 * Created by PhpStorm.
 * User: geolo
 * Date: 2018/8/27
 * Time: 14:04
 */
namespace app\modules\api\controllers;
use app\models\AgentGood;
use app\models\ShareGood;
use app\models\Goods;
use app\models\LiveStream;
use app\models\Store;
use app\models\LiveSetting;
use app\models\Agent;
use app\modules\api\models\IndexForm;

class  LiveController extends Controller
{
    public function actionProduct()
    {
        $form = new IndexForm();

        $form->store_id = \Yii::$app->request->get('store_id');
        $this->renderJson($form->getLiveData());
    }
    public function token($org_name,$app_name,$client_id,$client_secret){

        $url = "https://a1.easemob.com/".$org_name."/".$app_name."/token";
        $post_data = array ("grant_type" => "client_credentials","client_id" => $client_id,"client_secret"=>$client_secret);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post_data));
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        $output = curl_exec($ch);
        $err = curl_error($ch);
        $data = json_decode($output);
        return  $data->access_token;
        curl_close($ch);

    }
    public function getroomid($org_name,$app_name,$token){
        $url = "https://a1.easemob.com/".$org_name."/".$app_name."/liverooms?pagenum=1&pagesize=1";
        $head=array();
        $head[] ='Authorization: Bearer '.$token;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HTTPHEADER,$head);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        $output = curl_exec($ch);
        $err = curl_error($ch);
        $data = json_decode($output,true);

        return $data;
        curl_close($ch);

    }

    //获取直播所需的各种参数
    public function actionData(){
        //获取roomid\

        $wechat_app_id = \Yii::$app->request->get('store_id');
        //查詢平台id
        $pingtai_id=1;
        $query= Agent::find()->select('pingtai_id')->where(['store_id'=>$this->store_id])->asArray()->all();
//        var_dump($query);
        if(!empty($query)){
            $pingtai_id = $query[0]['pingtai_id'];
        }
        $data = LiveSetting::find()->where(['wechat_app_id'=>$wechat_app_id])->asArray()->one();
        $stream = LiveStream::find()->where(['wechat_app_id'=>$this->store_id])->asArray()->one();
        $res = ShareGood::find()->where(['store_id'=>$this->store_id])->select('good')->asArray()->all();
        $good_live = explode(',',$res[0]['good']);
        $product = Goods::find()->where(['store_id'=>$pingtai_id,'is_delete'=>0])->andWhere(['in','id',$good_live])->limit(3)->asArray()->all();
//        var_dump($product);
//        var_dump($stream);
        $photo= array();
        foreach($product as $value){
            $photo[]=$value['cover_pic'];
        }
        if($data['roomid']==''){
            $info = $this->getroomid($data['org_name'],$data['app_name'],$data['access_token']);
//            var_dump($info);
            if($info->error!=''){
                $token = $this->token($data['org_name'], $data['app_name'], $data['client_id'], $data['client_secret']);
                $info2 = $this->getroomid($data['org_name'],$data['app_name'],$data['access_token']);
                $roominfo = $info2['data'];
                $roomid = $roominfo['chatroom_id'];
                LiveSetting::updateAll(['access_token' => $token,'roomid'=>$data['roomid']], ['wechat_app_id' => $wechat_app_id]);
            }else{
//                var_dump($info);
                $roominfo = $info['data'];
                $roomid = $roominfo[0]["chatroom_id"];
            }
           $data["roomid"]=$roomid;
        }
        $data["pull_url"] = $stream["pull_stream"];
        $data["cover_pic"]=$photo;

       return json_encode($data);

    }
}